package com.zhijian.warehouse.manage.util;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;

public class DeviceDataConvert {
    /// 数据计算
    public static String dealBinaryStr(String str, Integer count) {
        if (count == null) {
            count = 1;
        }
        if (str.length() < 32) {
            System.out.println("qcb -- 数据长度不够");
            return "0";
        }
        String newStr = "";
        // 1位是符号位
        String firstStr = str.substring(0, 1);
        // 8位是指数位
        String secondStr = str.substring(1, 9);
        // 23位是尾数位
        String thirdStr = str.substring(9, 32);
        // IEEE574 计算
        newStr = "0".equals(firstStr) ? "" : "-";
        Integer secInt = Integer.parseInt(secondStr, 2) - 126;
        Integer thirdInt = Integer.parseInt("1" + thirdStr, 2);

        double upNum = Math.pow(2, secInt) * thirdInt;
        double lastNum = upNum / (256 * 65536);
        // 四舍五入，保留一位小数
        BigDecimal bigDecimal = new BigDecimal(newStr + String.valueOf(lastNum));
        BigDecimal divide = bigDecimal.divide(BigDecimal.ONE, count, RoundingMode.HALF_UP);
        return divide.toString();
    }


    /// 16进制转二进制 --- CD AB
    public static String hexToBinaryStrChangeSpace(String str) {
        String newStr = "";
        for (int i = str.length(); i > 0; i = i - 4) {
            String a = str.substring(i - 4, i);
            newStr += a;
        }
        String oldStr = new BigInteger(newStr, 16).toString(2);
//        String oldStr = Integer.toBinaryString(num);
        String returnStr = "";
        for (int i = oldStr.length(); i < str.length() * 4; i++) {
            returnStr += "0";
        }
        returnStr += oldStr;
        return returnStr;
    }

    /// 16进制转二进制，AB CD
    public static String hexToBinaryStr(String str) {
        String newStr = str;

        String oldStr = new BigInteger(newStr, 16).toString(2);
        String returnStr = "";
        for (int i = oldStr.length(); i < str.length() * 4; i++) {
            returnStr += "0";
        }
        returnStr += oldStr;
        return returnStr;
    }

    public static String convert(String msg, Integer count){
        String reactivePowerAll = hexToBinaryStrChangeSpace(msg);
        return dealBinaryStr(reactivePowerAll, count);
    }

    public static String convert1(String msg, Integer count){
        String reactivePowerAll = hexToBinaryStr(msg);
        return dealBinaryStr(reactivePowerAll, count);
    }

    public static String highToLower(String str){
        String newStr = "";
        for (int i = str.length(); i > 0; i = i - 4) {
            String a = str.substring(i - 4, i);
            newStr += a;
        }
        return newStr;
    }

    public static String intToHex(int n) {
        //StringBuffer s = new StringBuffer();
        StringBuilder sb = new StringBuilder(8);
        String a;
        char []b = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
        while(n != 0){
            sb = sb.append(b[n%16]);
            n = n/16;
        }
        a = sb.reverse().toString();
        return a;
    }
    /**
     * 去除首尾指定字符
     * @param str 字符串
     * @param element 指定字符
     * @return
     */
    public static String trimFirstAndLastChar(String str, String element){
        boolean beginIndexFlag = true;
        boolean endIndexFlag = true;
        do{
            int beginIndex = str.indexOf(element) == 0 ? 1 : 0;
            int endIndex = str.lastIndexOf(element) + 1 == str.length() ? str.lastIndexOf(element) : str.length();
            str = str.substring(beginIndex, endIndex);
            beginIndexFlag = (str.indexOf(element) == 0);
            endIndexFlag = (str.lastIndexOf(element) + 1 == str.length());
        } while (beginIndexFlag || endIndexFlag);
        return str;
    }

}
